<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Dashboard;
use App\Models\Depart;
use App\Models\User;

use App\Repos\DashboardRepo;
use App\Repos\DepartRepo;
use App\Models\MaintainType;
use App\Models\ServiceType;
use Session;
use DB,Log;
use App\Services\Wechat;
use App\Services\Msgtpl;
use Image;

class DepartController extends Controller
{
    public function index( Request $request)
    {
        $wx_user =  session('wechat.oauth_user.default');
        $rs = [];
        $user = User::where('openid',$wx_user->id)->first();
        if(!$user) {
            $rs['msg'] = '您还不是内部用户，请联系管理员获取内部用户绑定地址。';
            $rs['status'] = 'danger';
        }
        


        $where   = $param = [];        
        $query   = Depart::query();

        if($request->keyword) {
            $name = $param['keyword'] = $request->keyword; 
            $query->where('name','like','%'.$name.'%');
        }
      
        $list = $query->orderBy('id','desc')
                        ->paginate(16);
        
                
           
        return view('4s.store.depart-list',compact('list','param','rs'));
        
    }

    /**
     *  配件信息
     */
    public function info(Request $request)
    {     

        $wx_user    = session('wechat.oauth_user.default');
        $openid     = $wx_user->id;
        $worker     = User::where('openid',$openid)->first();
        if(!$worker) {
            $rs['msg']    = '没有权限';
            $rs['status'] = 'danger';
            return redirect('/user/depart')->with('rs',$rs);
        }
       
        $id = $request->id;
        
        $data   = Depart::find($id);
        return view('4s.depart-info',compact('data'));
    } 

    /**
       *    保存出库信息
       */  
    public function saveOut(Request $request)
    {
        $data = $request->all();    // data包含depart_id,depart_num
        $rs = DepartRepo::updateStore($data,3);
        //dd($rs);
        if($rs['status']) {
            $rs['msg']    = '出库成功';
            $rs['status'] = 'success';
            return redirect('/user/depart')->with('rs',$rs);
        }

        $rs['msg']    = $rs['msg'];
        $rs['status'] = 'danger';
        return back()->with('rs',$rs);


    }    
   

   
  


    
   
    
    



    


}
